<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="产品名称" prop="prodName">
        <el-input
          v-model="queryParams.prodName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属类别" prop="categoryId">
        <treeselect
          v-model="queryParams.categoryId"
          :options="categoryOptions"
          :normalizer="normalizerNew"
          placeholder="请选择"
          style="width:200px"
        />
      </el-form-item>
      <el-form-item label="检验状态" prop="checkStatus">
        <el-select 
          v-model="queryParams.checkStatus"
          placeholder="请选择检验状态"
        >
            <el-option
            v-for="dict in checkStatus"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            >
            </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:prod:add']"
          >新增
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['system:prod:edit']"-->
      <!--        >修改-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:prod:remove']"
          >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:prod:export']"
          >导出
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="prodList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品编码" align="center" prop="prodId" />
      <el-table-column label="产品名称" align="center" prop="prodName" />
      <el-table-column label="统称" align="center" prop="commonName" />
      <el-table-column label="所属类别" align="center" prop="categoryName" />
      <el-table-column label="检验状态" align="center" prop="checkStatus" >
        <template slot-scope="scope">
                    <dict-tag
                      :options="dict.type.prod_check_status"
                      :value="scope.row.checkStatus"
                    >
                    </dict-tag>
                  </template>
      </el-table-column>
      <el-table-column label="规格" align="center" prop="specification" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="库存数量" align="center" prop="prodInventory" />
      <el-table-column label="库存价" align="center" prop="purchasePrice" />
      <el-table-column label="底价" align="center" prop="floorPrice" />
      <!-- <el-table-column
        label="指导售价"
        align="center"
        prop="guideSellingPrice"
      /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:prod:edit']"
            >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:prod:remove']"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品编码" prop="prodId">
          <el-input v-if="isAdd == 1" v-model="form.prodId" placeholder="请输入产品编码" />
          <el-input v-else v-model="form.prodId" :disabled="true"/>
        </el-form-item>
        <el-form-item label="产品名称" prop="prodName">
          <el-input v-model="form.prodName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="统称" prop="commonName">
          <el-input v-model="form.commonName" placeholder="请输入统称" />
        </el-form-item>
        <el-form-item label="所属类别" prop="categoryId">
        <treeselect
          v-model="form.categoryId"
          :options="categoryOptions"
          :normalizer="normalizerNew"
          placeholder="请选择"
        />
      </el-form-item>
        <el-form-item label="规格" prop="specification">
          <el-input v-model="form.specification" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="库存数量" prop="prodInventory">
          <el-input-number v-model="form.prodInventory" placeholder="请输入库存数量" :min="0" :max="99999999" :precision="5"/>
        </el-form-item>
        <el-form-item label="采购价" prop="purchasePrice">
          <el-input-number v-model="form.purchasePrice" placeholder="请输入采购价" :min="0" :max="99999999" :precision="5"/>
        </el-form-item>
        <el-form-item label="底价" prop="floorPrice">
          <el-input-number v-model="form.floorPrice" placeholder="请输入底价" :min="0" :max="99999999" :precision="5"/>
        </el-form-item>
        <el-form-item label="指导售价" prop="guideSellingPrice">
          <el-input-number
            v-model="form.guideSellingPrice" placeholder="请输入指导售价" :min="0" :max="99999999" :precision="5"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addProd,
  delProd,
  getProd,
  listProd,
  updateProd,
} from "@/api/system/prod";
import { listCategoryTree } from "@/api/system/category";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Prod",
  components: { Treeselect },
  dicts: ['prod_check_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //是否为新增
      isAdd: 1,
      // 总条数
      total: 0,
      // 产品表格数据
      prodList: [],
      // 产品层级树选项
      categoryOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //产品检验状态字典
      checkStatus:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        prodName: null,
        categoryId: null,
        checkStatus: null,
        specification: null,
        prodInventory: null,
        purchasePrice: null,
        floorPrice: null,
        guideSellingPrice: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      prodId: [
          { required: true, message: '请输入产品编码', trigger: 'blur' },
          { min: 1, max: 20, message: '不能超过20个字符', trigger: 'blur' }
        ],
        prodName: [
          { required: true, message: '请输入产品名称', trigger: 'blur' },
          { min: 1, max: 20, message: '不能超过20个字符', trigger: 'blur' }
        ],
        categoryId: [
           { required: true, message: '请选择所属类别', trigger: 'change' }
        ],
        // specification: [
        //   { required: true, message: '请输入产品规格', trigger: 'blur' },
        //   { min: 1, max: 20, message: '不能超过20个字符', trigger: 'blur' }
        // ],
        prodInventory: [
          { required: true, message: '请输入产品库存', trigger: 'blur' },
        ],
        purchasePrice: [
          { required: true, message: '请输入采购价', trigger: 'blur' },
        ],
        floorPrice: [
          { required: true, message: '请输入底价', trigger: 'blur' },
        ],
        guideSellingPrice: [
          { required: true, message: '请输入指导售价', trigger: 'blur' },
        ]
      },
      //
    };
  },
  created() {
    this.getList();
    this.getCategoryListTree();
    this.getDicts("prod_check_status").then((res)=>{
      this.checkStatus = res.data;
      // console.log(this.checkStatus);
    })
  },
  methods: {
    /** 查询产品列表 */
    getList() {
      // console.log(this.queryParams);
      this.loading = true;
      listProd(this.queryParams).then((response) => {
        this.prodList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getCategoryListTree() {
      listCategoryTree().then((response) => {
        this.categoryOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    /** 转换数据结构 */
    normalizerNew(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.categoryId,
        label: node.categoryName,
        children: node.children,
      };
    },
    // 表单重置
    reset() {
      this.form = {
        prodId: null,
        prodName: null,
        categoryId: null,
        specification: null,
        prodInventory: null,
        purchasePrice: null,
        floorPrice: null,
        guideSellingPrice: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.prodId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品";
      this.isAdd = 1;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const prodId = row.prodId || this.ids;
      getProd(prodId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品";
        this.isAdd = 2;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.isAdd === 2) {
            updateProd(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const prodIds = row.prodId || this.ids;
      this.$modal
        .confirm('是否确认删除产品编号为"' + prodIds + '"的数据项？')
        .then(function () {
          return delProd(prodIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/prod/export",
        {
          ...this.queryParams,
        },
        `prod_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>